package org.example.fronted.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.Address;
import org.example.entity.Data;
import org.example.fronted.mapper.AddressMapper1;
import org.example.fronted.service.AddressService2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
@Transactional
public class AddressServiceImpl2 implements AddressService2 {

    @Resource
    private AddressMapper1 addressMapper1;

    public AddressServiceImpl2(AddressMapper1 addressMapper) {
        this.addressMapper1 = addressMapper;
    }

    @Override
    public Address queryById(Integer uid) {
        return (Address) addressMapper1.selectById(uid);
    }

    /**
     * 根据uid 先重置所有收货地址的状态为0，然后修改选中的收货地址为1
     * @param uId 用户ID
     * @param aId 地址ID
     * @return
     */
    @Override
    public Data changeTakeDeliveryAddress(Integer uId, Integer aId) {

        System.out.println("uid:"+uId + ",aid"+aId);

        //重置收货地址的状态都为 0
        int i = addressMapper1.resetTakeDeliveryAddress(uId);

        System.out.println("收货地址重置状态" + i);

        int i1 = addressMapper1.changeTakeDeliveryAddress(aId);

        System.out.println("收货地址修改状态" + i1);

        Data data = new Data();

        if (i == i1) {
            data.setCode(1);
            data.setMsg("修改成功");
            return data;
        }

        data.setCode(0);
        data.setMsg("修改失败");
        return data;
    }

    @Override
    public int defaultAddress(Integer uId, Integer aId) {
        //重置默认地址的状态都为 0
        int i = addressMapper1.resetDefaultAddress(uId);
        System.out.println("默认地址重置: " + i);
        //修改默认地址的状态为  1
        int i1 = addressMapper1.changeDefaultAddress(aId);
        System.out.println("默认地址修改: " + i1);
        if(i == i1){
            return 1;
        }
        return 0;
    }

    @Override
    public Address selectTDAddressByUid(Integer uId) {
        return null;
    }

    @Override
    public int insertAddress(Address address, Integer uId) {
        return 0;
    }

    @Override
    public List<Address> getAddressByUserId(Integer userId) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",userId);
        List<Address> list = addressMapper1.selectList(wrapper);
        System.out.println(list);
//        System.out.println(addressMapper.getAddressByUserId(userId));
        return list;
    }
}
